define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'superdata/activity/index' + location.search,
                    add_url: 'superdata/activity/add',
                    edit_url: 'superdata/activity/edit',
                    // del_url: 'superdata/activity/del',
                    multi_url: 'superdata/activity/multi',
                    import_url: 'superdata/activity/import',
                    table: 'activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left', operate: 'LIKE %...%', placeholder: '模糊搜索,*表示任意字符'},

                        {field: 'admin_id', title: __('Admin_id')},
                        {field: 'createTime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'startTime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'duration', title: __('Duration'),operate:false},
                        {field: 'category', title: __('Category')},
                        {field: 'tags', title: __('Tags'), operate: 'LIKE'},


                        {field: 'operate_id', title: __('Operate_id')},
                        {field: 'isticket_text', title: __('Isticket'), operate: false,formatter:function(value,row){
                                if (row['isticket'] == 1)
                                {
                                    return '<span style="color:yellowgreen">'+value+'</span>';
                                }else{
                                    return '<span style="color:blue">'+value+'</span>';
                                }
                            }
                        },
                        {field: 'status_text', title: __('Status'), operate: false,formatter:function(value,row){
                                if (row['status'] == 1)
                                {
                                    return '<span style="color:yellowgreen">'+value+'</span>';
                                }else {
                                    return '<span style="color:black">'+value+'</span>';
                                }
                            }
                        },
                        {
                            field: 'joinUrl',
                            title: '加入活动' ,
                            formatter: function (value, row, index) {
                                var ahref = '';
                                ahref += '<a class="btn btn-success btn-sm" target="_blank" href="'+row.joinUrl+'"><icon class="fa fa-play">加入活动</icon></a>';
                                return ahref;
                            }
                        },
                        {
                            field: 'status',
                            title: __('Status') ,
                            visible:false,
                            formatter: Table.api.formatter.normal,
                            searchList: { '0': '下架','1': '上架'},
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: function(row){
                                        if(row['status']===1) {
                                            return '下架';
                                        }else{
                                            return '上架';
                                        }
                                    },
                                    title: __('发送请求'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'vactivities/activity/detail?ids={ids}&status={status}',
                                    confirm: '确认发送操作请求？',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        table.bootstrapTable('refresh',{});
                                        return false;

                                    },
                                    error: function (data, ret) {
                                        //console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },


                                {
                                    name: 'detail',
                                    text: '详情',
                                    icon: 'fa fa-folder-o',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'vactivities/activity/detail?ids={ids}',
                                },
                                {
                                    name: 'execute',
                                    title: __('参与人'),
                                    text: __('参与人'),
                                    url: 'member/actrelacc/index?pack_id={ids}',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-execute btn-dialog'
                                },

                                {
                                    name: 'ajax',
                                    text: function(row){
                                        if(row['isrecommend']===1) {
                                            return '取消推荐';
                                        }else{
                                            return '推荐';
                                        }
                                    },
                                    title: __('发送请求'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'vactivities/activity/recommend?ids={ids}&isrecommend={isrecommend}',
                                    confirm: '确认发送操作请求？',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        table.bootstrapTable('refresh',{});
                                        return false;

                                    },
                                    error: function (data, ret) {
                                        //console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});